<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>//用于计算 array、map 或 普通 JavaScript 对象的权重；null 或者 未定义变量计 1 点；
// 基本类型计 2 点；对象或函数计 4 点。
function getCollectionWeight(collection) {
    let collectionValues;
    if (collection instanceof Array) {//如果输入变量是数组
//            debugger;
        collectionValues = collection;//是数组就将值赋给cv
    } else if (collection instanceof Map) {
        collectionValues = [...collection.values()
    ]
        ;
    } else {
        // debugger;
        collectionValues = Object.keys(collection).map(function (key) {
            return collection[key];
        });
    }
    console.log(collectionValues);
    return collectionValues.reduce(function (sum, item) {
        // debugger;
        if (item == null) {
            return sum + 1;
        }
        if (typeof item === 'object' || typeof item === 'function') {
            return sum + 4;
        }
        return sum + 2;
    }, 0);
};
let myArray = [null, {}, 15];
let myMap = new Map([['functionKey', function () {
}]]);
console.log(myMap);//
let myObject = {'stringKey': 'Hello world'};
console.log(getCollectionWeight(myArray));  // => null和对象和基本类型7 (1 + 4 + 2)
console.log(getCollectionWeight(myMap));    // => 4对象
console.log(getCollectionWeight(myObject)); // => 2基本类型
</script>
</body>
</html>